package org.projects.graduates.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.lang3.builder.HashCodeBuilder;

import com.dayatang.domain.AbstractEntity;
import com.dayatang.domain.QuerySettings;


@Entity
@Table(name="replies_")
public class Reply extends AbstractEntity {

	private static final long serialVersionUID = -8142253448640338901L;

	@OneToOne
	private Person who;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="up_date")
	private Date upDate;

	@Lob
	private String content;
	
	public static List<Reply> findBy(Post replyTo){
		List<Reply> results = new ArrayList<Reply>();
		
		QuerySettings<Reply> settings = QuerySettings.create(Reply.class);
		
		settings.eq("replyTo", replyTo).desc("id");
		
		results.addAll(getRepository().find(settings));
		
		return results;
	}
	
	
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(this.getId()).toHashCode();
	}

	@Override
	public boolean equals(Object other) {
		if(null == other){
			return false;
		}
		if(other instanceof Reply){
			Reply o = (Reply)other;
			if(o.getId() ==this.getId()){
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return "content : " + content + " update : " + upDate;
	}

	public Person getWho() {
		return who;
	}

	public void setWho(Person who) {
		this.who = who;
	}

	public Date getUpDate() {
		return upDate;
	}

	public void setUpDate(Date upDate) {
		this.upDate = upDate;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}
